﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using LdExpress;
using System.IO;
using System.Web.UI.WebControls.WebParts;

public partial class _Default : System.Web.UI.Page
{
    public string ContainerPage = "./Tasking.aspx";
    public string strCondition = "";
    public int NumOfItemsPerPage = 20;
    public string strPager = "";
    public string strError = "";
    public string strMess = "";
    public string strProcessName = "";
    public int nPermission = 0;
    public int nPage = 1;
    public string strPageID = "page_number";
    public string oldComment = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

            if (Request.QueryString[strPageID] != null)
                nPage = int.Parse(Request.QueryString[strPageID]);
        }
        catch
        {
            nPage = 1;
        }
        if (nPage <= 0) nPage = 1;
        if (Session["changFiterCoding"] == null) Session["changFiterCoding"] = "-1";
        if (Session["logined_id"] != null)
        {
            if (int.Parse(Session["tasking"].ToString()) > 0) nPermission = int.Parse(Session["tasking"].ToString());
            if (int.Parse(Session["superadmin"].ToString()) > 0) nPermission = 10;
            if (nPermission < 3) Response.Redirect("Login.aspx");

        }
        else
            Response.Redirect("Login.aspx");

        if (!Page.IsPostBack)
        {
            //loadDropdownFiterCoding();
            drFiterCoding.SelectedValue = Session["changFiterCoding"].ToString();
            if (Session["sessTaskKeyword"] != null)
                txtKeyword.Text = Session["sessTaskKeyword"].ToString();
            if (Request.QueryString["action"] == "delete" && nPermission > 3)
            {
                Tasking obj = new Tasking();
                obj.ID = long.Parse(Request.QueryString["ID"]);
                obj.Load(obj.ID, GV.cn, GV.trans);
                obj.Delete(GV.cn, GV.trans);
                Response.Redirect("Tasking.aspx" + ((Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "") ? "?page_number=" + Request.QueryString["page_number"].ToString() : ""));
            }
            if (nPermission > 0)
            {
                #region Build List
                int nNumOfPages = 0;

                DataTable dtList;
                BuildList(strCondition, NumOfItemsPerPage, nPage, out nNumOfPages, out dtList, null, null);
                if (nPage > nNumOfPages) nPage = 1;

                rptData.DataSource = dtList;
                rptData.DataBind();
                #endregion
                #region Build Pager
                CommonFunctions.BuildPager(Request, nNumOfPages, nPage, strPageID, ContainerPage, out strPager);
                #endregion
            }
        }

    }

    private void BuildList(string strCondition, int NumOfItemsPerPage, int PageNo, out int nNumOfPages, out DataTable dtList, MySqlConnection cn, MySqlTransaction trans)
    {
        DataSet ds = null;
        dtList = new DataTable();
        dtList.Columns.Add("Index");
        dtList.Columns.Add("Sort");
        dtList.Columns.Add("Comment");
        dtList.Columns.Add("FileName1");
        dtList.Columns.Add("CreatedDate");
        dtList.Columns.Add("Detail");
        dtList.Columns.Add("FileName2");
        dtList.Columns.Add("LastUpdatedDate");
        dtList.Columns.Add("SourceName");
        dtList.Columns.Add("Summary");
        dtList.Columns.Add("priority");
        dtList.Columns.Add("IsCompleted");
        dtList.Columns.Add("Action");
        //Get List by SoIDs
        nNumOfPages = 0;
        //string strCondition = "";
        Tasking obj = new Tasking();
        string strOrderBy = "priority desc,CreatedDate desc";
        if (Session["sessTaskKeyword"] != null && Session["sessTaskKeyword"].ToString() != "")
        {
            strCondition = " AND (Comment like '%" + DBModule.RefineString(Session["sessTaskKeyword"].ToString()) + "%'" +
                " OR Detail like '%" + DBModule.RefineString(Session["sessTaskKeyword"].ToString()) + "%')";
        }
        else
        {
            if (Session["changFiterCoding"] != null && Session["changFiterCoding"].ToString() != "-1")
            {
                strCondition += " and IsCompleted='" + Session["changFiterCoding"].ToString() + "'";
                if (Session["changFiterCoding"].ToString() == "4")
                    strOrderBy = "LastUpdatedDate DESC";
            }
            else
                strCondition += " and IsCompleted <4 ";
        }
        obj.GetList(strCondition, strOrderBy, NumOfItemsPerPage, PageNo, out nNumOfPages, out ds, GV.cn, GV.trans);

        string strAllQueryString = "";

        foreach (string strKey in Request.QueryString.AllKeys)
        {
            if (strKey != null)
            {
                if ((strKey.ToLower() != "action") && (strKey.ToLower() != "id"))
                    strAllQueryString = strAllQueryString + "&" + strKey + "=" + Request.QueryString[strKey].ToString();
            }
        }

        if (strAllQueryString.Length >= 1)
            strAllQueryString = strAllQueryString.Substring(1);

        if (ds != null)
        {
            int count = 0;
            if (Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "")
            {
                int pagenumber = 1;
                try
                {
                    pagenumber = int.Parse(Request.QueryString["page_number"]);
                    count = NumOfItemsPerPage * (pagenumber - 1);
                }
                catch { count = 0; }
            }
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                count++;
                string strSTT = dr["Sort"].ToString();
                string strComment = dr["Comment"].ToString().Trim();
                string strFile1 = dr["FileName1"].ToString();
                if (strFile1 != null & strFile1 != "")
                    strFile1 = "<a href=\"/Tasking/" + strFile1 + "\">Download</a>";
                string strCreatedDate = Convert.ToDateTime(dr["CreatedDate"]).ToString("dd-MM-yyyy HH:mm:ss");
                string strDesc = DBModule.RefineString(dr["Detail"].ToString());
                string strFile2 = dr["FileName2"].ToString();
                if (strFile2 != null & strFile2 != "")
                    strFile2 = "<a href=\"/Tasking/" + strFile2 + "\">Download</a>";
                string strLastUpdatedDate = Convert.ToDateTime(dr["LastUpdatedDate"]).ToString("dd-MM-yyyy HH:mm:ss");
                string strSourceName = dr["SourceName"].ToString();
                string strDateEstimate = dr["Summary"].ToString();
                string strpriority = dr["priority"].ToString();
                string strIsCompleted = dr["IsCompleted"].ToString();

                //if (strName == "") strCreatedDate = "";
                strDateEstimate = strDateEstimate != "" ? strDateEstimate + " ngày" : "";
                string strAction = "";
                if (nPermission > 0)
                {
                    strAction = "<a href=\"#\" onclick=\"openTaskEdit(" + dr["ID"].ToString() + ");\">Edit</a>";

                }
                if (nPermission > 3)
                {
                    strAction += " - <a href=\"javascript:doDelete('" + dr["ID"].ToString() + "','')\">" + "Delete</a>";
                }
                string[] strValues = new string[] { count.ToString(), strSTT, strComment, strFile1, strCreatedDate, strDesc, strFile2, strLastUpdatedDate, strSourceName, strDateEstimate, strpriority, strIsCompleted, strAction };
                dtList.Rows.Add(strValues);
            }
        }
    }
    public string GetCodingPeriod(object Coding)
    {
        string retVal = "";
        switch (Coding.ToString())
        {
            case "0":
                retVal = "<div style=\"color:#01a46d;font-weight:bold;text-align:center;\">Open</\"div\">";
                break;
            case "1":
                retVal = "<div style=\"color:#ff9b2b;font-weight:bold;text-align:center;\">Start</\"div\">";
                break;
            case "2":
                retVal = "<div style=\"color:#f50017;font-weight:bold;text-align:center;\">Not Complete</\"div\">";
                break;
            case "3":
                retVal = "<div style=\"color:#ec3e40;font-weight:bold;text-align:center;\">Completed</\"div\">";
                break;
            case "4":
                retVal = "<div style=\"color:#f5d800;font-weight:bold;text-align:center;\">Closed</\"div\">";
                break;
            case "5":
                retVal = "<div style=\"color:#0000;font-weight:bold;text-align:center;\">Archived</\"div\">";
                break;
        }
        return retVal;
    }

    //protected void loadDropdownFiterCoding()
    //{
    //    string sql = "";
    //    DataSet ds = null;

    //    sql = "SELECT DISTINCT IsCompleted," +
    //    "case IsCompleted" +
    //      " when \"0\" then \"Open\" " +
    //      " when \"1\" then \"Start\"" +
    //      " when \"2\" then \"Started\"" +
    //    "when \"3\" then \"Done\"" +
    //    "when \"4\" then \"Closed\"" +
    //    "end as field " +
    //    "from tbl_tasking";
    //    ds = DBModule.ExecuteQuery(sql, GV.cn, GV.trans);
    //    DataTable dtb = ds.Tables[0];
    //    drFiterCoding.DataSource = dtb;
    //    drFiterCoding.DataValueField = dtb.Columns[0].ToString();
    //    drFiterCoding.DataTextField = dtb.Columns[1].ToString();
    //    drFiterCoding.DataBind();
    //    drFiterCoding.Items.Insert(0, new ListItem("Load Coding", ""));
    //    drFiterCoding.Items.Insert(1, new ListItem("All", "All"));
    //    if (Session["changFiterCoding"] != null && ds.Tables[0].Rows.Count > 0)
    //    {
    //        try
    //        {
    //            drFiterCoding.SelectedValue = Session["changFiterCoding"].ToString();
    //        }
    //        catch { }
    //    }
    //}
    protected void drFiterCoding_Selected(object sender, EventArgs e)
    {
        Session["changFiterCoding"] = drFiterCoding.SelectedValue;
        Response.Redirect("Tasking.aspx");
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["sessTaskKeyword"] = txtKeyword.Text.Trim();
        Session["changFiterCoding"] = "-1";
        Response.Redirect("Tasking.aspx");
    }
}
